package com.nhutcm.sms.network;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;

import com.nhutcm.sms.Utils.Constant;
import com.nhutcm.sms.Utils.StringUtils;
import com.nhutcm.sms.model.SMSClass;
import com.nhutcm.sms.service.SMSBroadcastingService;

public class SMSBroadcastingNetwork {

	public static String REQ_CODE = "REQ_CODE";
	public static String SMS_VALUE = "SMS_VALUE";
	public static String URL = "URL";
	public static String SMS_ARRAY = "SMS_ARRAY";
	public static String UPDATE_ARRAY = "UPDATE_ARRAY";
	public static String TAG = "SMSBroadcastingNetwork";

	public static void sendPingRequest(Context context) {
		Intent i = new Intent(context, SMSBroadcastingService.class);
		i.putExtra(REQ_CODE, Constant.PING_REQ_CODE);
		context.startService(i);
	}

	public static void sendGetListRequest(Context context) {
		Intent i = new Intent(context, SMSBroadcastingService.class);
		i.putExtra(REQ_CODE, Constant.GET_LIST_NUM_REQ_CODE);
		context.startService(i);
	}

	public static void updateStatus(Context context, SMSClass sms) {

		Intent i = new Intent(context, SMSBroadcastingService.class);
		i.putExtra(REQ_CODE, Constant.UPDATE_STATUS_CODE);

		ArrayList<String> params = new ArrayList<String>();
		params.add(sms.getSmsno());
		params.add(sms.getPhone());
		params.add(sms.getStatus());
		i.putStringArrayListExtra(UPDATE_ARRAY, params);
		context.startService(i);

	}

	public static void forwardMessage(Context context, SMSClass sms) {
		Intent i = new Intent(context, SMSBroadcastingService.class);
		i.putExtra(REQ_CODE, Constant.FORWARD_MESSAGE_CODE);

		ArrayList<String> params = new ArrayList<String>();
		params.add(sms.getPhone());
		params.add(sms.getMessage());
		i.putStringArrayListExtra(SMS_ARRAY, params);
		context.startService(i);
	}

	public static void sendSMS(Context context, SMSClass sms) {
		Intent i = new Intent(context, SMSBroadcastingService.class);
		i.putExtra(REQ_CODE, Constant.SEND_SMS);
		i.putExtra(SMS_VALUE, sms);
		context.startService(i);
	}

	public static void sendLongSMS(Context context, SMSClass sms) {

		String message = sms.getMessage();
		List<String> arr = StringUtils.splitByLength(message,
				Constant.MAX_NUM_CHARS_PER_MESSAGE);
		int size = arr.size();
		for (int i = 0; i < size; i++) {
			SMSClass smsChild = new SMSClass(sms.getPhone(), sms.getSmsno(),
					arr.get(i));
			sendSMS(context, smsChild);
		}

	}

	public static void sendPointMessage(Context context) {
		Intent i = new Intent(context, SMSBroadcastingService.class);
		i.putExtra(REQ_CODE, Constant.POINT_MESS);
		context.startService(i);
	}
}
